package com.css.fxfzfxqh.modules.buildingResetUnitPrice.repository.impl;

import com.css.fxfzfxqh.constants.FxfzConstants;
import com.css.fxfzfxqh.modules.buildingResetUnitPrice.entity.BuildingResetUnitPriceEntity;
import com.css.fxfzfxqh.modules.buildingResetUnitPrice.repository.BuildingResetUnitPriceRepository;
import com.css.fxfzfxqh.modules.buildingResetUnitPrice.vo.ParamsVo;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class BuildingResetUnitPriceRepositoryImpl implements BuildingResetUnitPriceRepository {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public boolean save(BuildingResetUnitPriceEntity buildingResetUnitPriceEntity) {
        String sql = "INSERT INTO fxqh_building_reset_unit_price VALUES (:id,:province,:provinceId,:city,:cityId,:county,:countyId,:brickConcrete,:stoneWood,:brickWood,:reinforcedConcreteFrame,:reinforcedConcreteFrameShearWall,:reinforcedConcreteShearWall,:soilWood,:wood,:steel,:industrialBrickConcrete,:industrialReinforcedConcrete,:industrialSteel,:composite,:other,:delFlag,:createName,:createUser,:createTime,:updateName,:updateUser,:updateTime)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(buildingResetUnitPriceEntity);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }


    @Override
    public Map<String, Object> page(ParamsVo paramsVo, int curPage, int pageSize, String allRole, String provinceId) {
        StringBuilder sql = new StringBuilder("select * from fxqh_building_reset_unit_price ");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
        if(allRole.contains(FxfzConstants.YPG_PRI_GL_ROLE) || allRole.contains(FxfzConstants.YPG_PRI_YW_ROLE)){//省级
            whereSql.append(" and province_id = '"+provinceId+"'");
        }
        if(!PlatformObjectUtils.isEmpty(paramsVo.getProvinceId())){
            whereSql.append(" and province_id = '"+paramsVo.getProvinceId()+"'");
        }
        if(!PlatformObjectUtils.isEmpty(paramsVo.getCityId())){
            whereSql.append(" and city_id = '"+paramsVo.getCityId()+"'");
        }
        if(!PlatformObjectUtils.isEmpty(paramsVo.getCountyId())){
            whereSql.append(" and county_id = '"+paramsVo.getCountyId()+"'");
        }

        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        sql.append(whereSql).append(orderSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(BuildingResetUnitPriceEntity.class)).size();
        List<BuildingResetUnitPriceEntity> samplingData = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(BuildingResetUnitPriceEntity.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", samplingData);
        return map;
    }

    @Override
    public boolean update(BuildingResetUnitPriceEntity buildingResetUnitPriceEntity) {
        String sql = "update fxqh_building_reset_unit_price set province=:province,province_id=:provinceId,city=:city,city_id=:cityId,county=:county,county_id=:countyId," +
                "brick_concrete=:brickConcrete,stone_wood=:stoneWood,brick_wood=:brickWood,reinforced_concrete_frame=:reinforcedConcreteFrame,reinforced_concrete_frame_shear_wall=:reinforcedConcreteFrameShearWall," +
                "reinforced_concrete_shear_wall=:reinforcedConcreteShearWall,soil_wood=:soilWood,wood=:wood,steel=:steel,industrial_brick_concrete=:industrialBrickConcrete," +
                "industrial_reinforced_concrete=:industrialReinforcedConcrete,industrial_steel=:industrialSteel,composite=:composite,other=:other," +
                "update_name=:updateName,update_user=:updateUser,update_time=:updateTime where id='"+buildingResetUnitPriceEntity.getId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(buildingResetUnitPriceEntity);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }

    @Override
    public boolean delete(String id) {
        String sql = "update fxqh_building_reset_unit_price set del_flag = '1' where id = ?";
        return jdbcTemplate.update(sql,id) > 0;
    }

    @Override
    public BuildingResetUnitPriceEntity findById(String id) {
        String sql = "select * from fxqh_building_reset_unit_price where del_flag = '0' and id = ?";
        try {
            BuildingResetUnitPriceEntity buildingResetUnitPriceEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(BuildingResetUnitPriceEntity.class),id);
            return buildingResetUnitPriceEntity;
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    @Override
    public BuildingResetUnitPriceEntity findByDistrict(String province, String city, String county) {

        StringBuffer sql = new StringBuffer("select * from fxqh_building_reset_unit_price where del_flag = '0' ");
        if(PlatformObjectUtils.isNotEmpty(province)){
            sql.append("and province = '"+province+"' ");
        }if(PlatformObjectUtils.isNotEmpty(city)){
            sql.append("and city = '"+city+"' ");
        } if(PlatformObjectUtils.isNotEmpty(county)){
            sql.append("and county = '"+county+"' ");
        }
        List<BuildingResetUnitPriceEntity> buildingResetUnitPriceEntitys = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(BuildingResetUnitPriceEntity.class));
        return PlatformObjectUtils.isNotEmpty(buildingResetUnitPriceEntitys) && buildingResetUnitPriceEntitys.size()>0?buildingResetUnitPriceEntitys.get(0):null;
    }
    @Override
    public BuildingResetUnitPriceEntity findByPronvince(String province, String city, String county) {

        StringBuffer sql = new StringBuffer("select * from fxqh_building_reset_unit_price where del_flag = '0' ");
        if(PlatformObjectUtils.isNotEmpty(province)){
            sql.append("and province_id = '"+province+"' ");
        }if(PlatformObjectUtils.isNotEmpty(city)){
            sql.append("and city_id = '"+city+"' ");
        } if(PlatformObjectUtils.isNotEmpty(county)){
            sql.append("and county_id = '"+county+"' ");
        }
        List<BuildingResetUnitPriceEntity> buildingResetUnitPriceEntitys = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(BuildingResetUnitPriceEntity.class));
        return PlatformObjectUtils.isNotEmpty(buildingResetUnitPriceEntitys) && buildingResetUnitPriceEntitys.size()>0?buildingResetUnitPriceEntitys.get(0):null;
    }

    @Override
    public BuildingResetUnitPriceEntity findByPronvinceName(String province, String city, String county) {

        StringBuffer sql = new StringBuffer("select * from fxqh_building_reset_unit_price where del_flag = '0' ");
        if(PlatformObjectUtils.isNotEmpty(province)){
            sql.append("and province = '"+province+"' ");
        }if(PlatformObjectUtils.isNotEmpty(city)){
            sql.append("and city = '"+city+"' ");
        }
        if(PlatformObjectUtils.isNotEmpty(county)){
            sql.append("and county = '"+county+"' ");
        }
        List<BuildingResetUnitPriceEntity> buildingResetUnitPriceEntitys = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(BuildingResetUnitPriceEntity.class));
        return PlatformObjectUtils.isNotEmpty(buildingResetUnitPriceEntitys) && buildingResetUnitPriceEntitys.size()>0?buildingResetUnitPriceEntitys.get(0):null;
    }


}
